package wrapper

import (
	"gitee.com/ymofen/gobase"
	"github.com/suiyunonghen/dxsvalue"
)

func (this *SuperValue) Length() int {
	return this.val.Count()
}

func (this *SuperValue) AsRecord(idx int) *SuperValue {
	return this.ValueByIndex(idx)
}

func (this *SuperValue) AsRecordByName(name string) *SuperValue {
	return this.ValueByName(name)
}

func (this *SuperValue) CloneCache(usecache bool) *SuperValue {
	val := this.val.Clone(usecache)
	rval := NewSVFromVal(val)
	rval.useCache = usecache
	return rval
}

func (this *SuperValue) Clone() *SuperValue {
	return this.CloneCache(true)
}

func (this *SuperValue) ToString() string {
	return this.String()
}

func (this *SuperValue) AsFloatByPath(path string, def float32) float32 {
	str := this.StringByPath(path, "")
	return gobase.StrToFloat32Def(str, def)
}

func (this *SuperValue) SetString(path string, val string) {
	this.ForceByPath(path).val.SetString(val)
}

func (this *SuperValue) SetPathValue(path string, val interface{}) {
	this.ForceByPath(path).val.SetValue(val)
}

func (this *SuperValue) SetInt(path string, val int) {
	this.ForceByPath(path).val.SetInt(int64(val))
}

func (this *SuperValue) SetInt64(path string, val int64) {
	this.ForceByPath(path).val.SetInt(val)
}

func (this *SuperValue) SetDouble(path string, val float64) {
	this.ForceByPath(path).val.SetDouble(val)
}

func (this *SuperValue) SetFloat(path string, val float32) {
	this.ForceByPath(path).val.SetFloat(val)
}

func (this *SuperValue) AsRecordByPath(path string) *SuperValue {
	return this.ValueByPath(path)
}

func (this *SuperValue) SetKeyRecord(path string, rec *SuperValue) {
	this.ForceByPath(path).val.CopyFrom(rec.val, this.val.ValueCache())
}

func (this *SuperValue) JsonParserFromByte(buf []byte) error {
	val, err := dxsvalue.NewValueFromJson(buf, true, true)
	if err != nil {
		return err
	}
	this.val.CopyFrom(val, this.val.ValueCache())
	return nil
}

func (this *SuperValue) JsonParserFromFile(filename string) error {
	fileData, err := gobase.ReadFile(filename)
	if err != nil {
		return err
	}
	return this.JsonParserFromByte(fileData)
}

func (this *SuperValue) JsonParserFromByte2(buf []byte, ConvertEscape, structRest bool) error {
	val, err := dxsvalue.NewValueFromJson(buf, false, false)
	if err != nil {
		return err
	}
	this.val.CopyFrom(val, this.val.ValueCache())
	return nil
}
